/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cw.ulti;

import cw.biz.UserBiz;
import cw.entity.User;
import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author VinhTC
 */
public class Helper implements Serializable {

    /**
     * encryptor
     * @param txtToEncrypt
     * @return
     */
    public String encryptor(String txtToEncrypt) {
        String result = null;

        return result;
    }

    /**
     * Helper
     */
    public Helper() {
    }

    /**
     * processMail
     * @param to
     * @param subject
     * @param content
     * @return
     */
    public boolean processMail(String to, String subject, String content) {
        boolean flag = false;
        User user = new User();
        UserBiz ubiz = new UserBiz();
        user = ubiz.getUserByEmail(to);
        if (user != null) {
            subject = "Recover password for: " + to;

            content = "This mail form CROSS WORDS support center.\nThis is recover password mail, "
                    + "below is your username and password\n \nUserName: " + user.getUserName() + "\nPassword: " + user.getPassword()
                    + "\n\nKindly don't reply this mail"
                    + "\n\nBest regards,"
                    + "\nCross Words Support Center";
            final String fromMail = "dragongroup2011@gmail.com";
            //Kindly keep it private.
            final String passwd = "dragon123456";

            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");

            Session session = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {

                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(fromMail, passwd);
                        }
                    });
            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(fromMail));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(to));
                message.setSubject(subject);

                message.setText(content);

                Transport.send(message);

                flag = true;
            } catch (MessagingException e) {
                Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        return flag;
    }
}
